import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFactorySpi;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.postgis.PostgisNGDataStoreFactory;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.action.SafeAction;
import org.geotools.swing.data.JDataStoreWizard;
import org.geotools.swing.wizard.JWizard;
import org.opengis.feature.simple.SimpleFeature;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class Query extends JFrame {
    private DataStore dataStore;
    private JTextField text;

    public static void main(String[] args) throws IOException {
        showFrame3();
    }

    public static void showFrame3() throws IOException {
        Map<String, Object> params = new HashMap<String, Object>();
        DataStore pgDatastore = null;
        params.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgis"); //需要连接何种数据库，postgis or mysql
        params.put(PostgisNGDataStoreFactory.HOST.key, "localhost");//ip地址
        params.put(PostgisNGDataStoreFactory.PORT.key, new Integer(5432));//端口号
        params.put(PostgisNGDataStoreFactory.DATABASE.key, "test");//需要连接的数据库
        params.put(PostgisNGDataStoreFactory.SCHEMA.key, "public");//架构
        params.put(PostgisNGDataStoreFactory.USER.key, "postgres");//需要连接数据库的名称
        params.put(PostgisNGDataStoreFactory.PASSWD.key, "postgres");//数据库的密码

        pgDatastore = DataStoreFinder.getDataStore(params);
        SimpleFeatureSource featureSource = pgDatastore.getFeatureSource("river");
//        SimpleFeatureIterator iterator = featureSource.getFeatures().features();
//        while (iterator.hasNext()){
//            SimpleFeature feature = iterator.next();
//            System.out.println(feature.getDefaultGeometry());
//        }

//        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        Style style = SLD.createLineStyle(Color.BLACK, 1);
        FeatureLayer featureLayer = new FeatureLayer(featureSource, style);

        MapContent map = new MapContent();
        map.addLayer(featureLayer);

        JMapFrame.showMap(map);


//        JDataStoreWizard wizard = new JDataStoreWizard();
//        int result = wizard.showModalDialog();
//        if(result == JWizard.FINISH){
//            Map<String, Object> connectionParameters = wizard.getConnectionParameters();
//            DataStore dataStore = DataStoreFinder.getDataStore(connectionParameters);
//        }
    }
}